package com.ruoyi.channel.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.db.domain.MemberSourceInfo;
import com.ruoyi.db.domain.SourcePushResultInfo;
import com.ruoyi.db.mapper.MemberSourceInfoMapper;
import com.ruoyi.db.mapper.SourcePushResultInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.ChannelCountInfoMapper;
import com.ruoyi.db.domain.ChannelCountInfo;
import com.ruoyi.channel.service.IChannelCountInfoService;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 渠道统计信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-01
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class ChannelCountInfoServiceImpl implements IChannelCountInfoService 
{
    @Autowired
    private ChannelCountInfoMapper channelCountInfoMapper;

    @Autowired
    private SourcePushResultInfoMapper sourcePushResultInfoMapper;

    @Autowired
    private MemberSourceInfoMapper memberSourceInfoMapper;

    /**
     * 查询渠道统计信息
     * 
     * @param id 渠道统计信息主键
     * @return 渠道统计信息
     */
    @Override
    public ChannelCountInfo selectChannelCountInfoById(Long id)
    {
        return channelCountInfoMapper.selectChannelCountInfoById(id);
    }

    /**
     * 查询渠道统计信息列表
     * 
     * @param channelCountInfo 渠道统计信息
     * @return 渠道统计信息
     */
    @Override
    public List<ChannelCountInfo> selectChannelCountInfoList(ChannelCountInfo channelCountInfo)
    {
        return channelCountInfoMapper.selectChannelCountInfoList(channelCountInfo);
    }

    /**
     * 查询渠道统计信息列表
     *
     * @param channelCountInfo 渠道统计信息
     * @return 渠道统计信息
     */
    @Override
    public List<Map<String, Object>> selectChannelCountInfoMapList(ChannelCountInfo channelCountInfo) {

        List<Map<String, Object>> list = channelCountInfoMapper.selectChannelCountInfoMapList(channelCountInfo);

        SourcePushResultInfo sourcePushResultInfo = new SourcePushResultInfo();
        sourcePushResultInfo.setStartDateInfo(channelCountInfo.getStartDateInfo());
        sourcePushResultInfo.setEndDateInfo(channelCountInfo.getEndDateInfo());
        sourcePushResultInfo.setChannelCode(channelCountInfo.getChannelCode());
        sourcePushResultInfo.setChannelName(channelCountInfo.getChannelName());

        List<Map<String, Object>> list2 = sourcePushResultInfoMapper.selectAccountMapList1(sourcePushResultInfo);


        Map<String, Object> costMap = new HashMap<>();
        for (Map<String, Object> item : list2) {
            String channelCode = (String) item.get("channelCode");
            String dateInfo = (String) item.get("dateInfo");
            Object cost = item.get("cost");
            String key = channelCode + "_" + dateInfo;
            costMap.put(key, cost);
        }


        for (Map<String, Object> item : list) {
            String channelCode = (String) item.get("channelCode");
            String dateInfo = (String) item.get("dateInfo");
            String key = channelCode + "_" + dateInfo;
            if (costMap.containsKey(key)) {
                item.put("uvCost", costMap.get(key));
            }
        }


        MemberSourceInfo memberSourceInfo = new MemberSourceInfo();
        memberSourceInfo.setEndDateInfo(channelCountInfo.getEndDateInfo());
        memberSourceInfo.setStartDateInfo(channelCountInfo.getStartDateInfo());
        memberSourceInfo.setChannelCode(channelCountInfo.getChannelCode());


        List<Map<String, Object>> list3 = memberSourceInfoMapper.selectWuYongCount(memberSourceInfo);

        // 将 list3 的 value 数据加入到 list 中
        Map<String, Object> wuYongMap = new HashMap<>();
        for (Map<String, Object> item : list3) {
            String channelName = (String) item.get("channelName");
            String dateInfo = (String) item.get("dateInfo");
            Object value = item.get("value");
            String key = channelName + "_" + dateInfo; // 使用 channelName 和 dateInfo 作为唯一键
            wuYongMap.put(key, value);
        }

        // 遍历 list，查找匹配的 channelName 和 dateInfo，并添加 value
        for (Map<String, Object> item : list) {
            String channelName = (String) item.get("channelName"); // 获取 channelName
            String dateInfo = (String) item.get("dateInfo"); // 获取 dateInfo
            String key = channelName + "_" + dateInfo; // 生成匹配键
            if (wuYongMap.containsKey(key)) {
                item.put("wuYongValue", wuYongMap.get(key)); // 添加 value 到 list
            }
        }

        return list;
    }





    @Override
    public List<Map<String, Object>> selectChannelPage(ChannelCountInfo channelCountInfo)
    {
        return channelCountInfoMapper.selectChannelCountInfoMapList(channelCountInfo);
    }
    @Override
    public Map<String, Object> selectChannelCountInfoAllMapList(ChannelCountInfo channelCountInfo)
    {
        return channelCountInfoMapper.selectChannelCountInfoAllMapList(channelCountInfo);
    }

    /**
     * 新增渠道统计信息
     * 
     * @param channelCountInfo 渠道统计信息
     * @return 结果
     */
    @Override
    public int insertChannelCountInfo(ChannelCountInfo channelCountInfo)
    {
        channelCountInfo.setCreateTime(DateUtils.getNowDate());
        return channelCountInfoMapper.insertChannelCountInfo(channelCountInfo);
    }

    /**
     * 修改渠道统计信息
     * 
     * @param channelCountInfo 渠道统计信息
     * @return 结果
     */
    @Override
    public int updateChannelCountInfo(ChannelCountInfo channelCountInfo)
    {
        channelCountInfo.setUpdateTime(DateUtils.getNowDate());
        return channelCountInfoMapper.updateChannelCountInfo(channelCountInfo);
    }

    /**
     * 批量删除渠道统计信息
     * 
     * @param ids 需要删除的渠道统计信息主键
     * @return 结果
     */
    @Override
    public int deleteChannelCountInfoByIds(Long[] ids)
    {
        return channelCountInfoMapper.deleteChannelCountInfoByIds(ids);
    }

    /**
     * 删除渠道统计信息信息
     * 
     * @param id 渠道统计信息主键
     * @return 结果
     */
    @Override
    public int deleteChannelCountInfoById(Long id)
    {
        return channelCountInfoMapper.deleteChannelCountInfoById(id);
    }

    @Override
    public List<Map<String, Object>> getWeComCount(ChannelCountInfo channelCountInfo) {
        return channelCountInfoMapper.getWeComCount(channelCountInfo);
    }

    @Override
    public int deleteChannelCountInfoByDateInfo(String dateInfo) {
        return channelCountInfoMapper.deleteChannelCountInfoByDateInfo(dateInfo);
    }

    @Override
    public List<Map<String, Object>> getUvCost(ChannelCountInfo channelCountInfo) {
        return channelCountInfoMapper.getUvCost(channelCountInfo);
    }
}
